import type { RouteRecordRaw } from 'vue-router'

// 公共路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'LoginLayout',
    component: () => import('@/layouts/LoginLayout.vue'),
    meta: {
      requiresAuth: false
    },
    children: [
      {
        path: '',
        name: 'Login',
        component: () => import('@/views/Login/LoginView.vue'),
        meta: {
          title: '登录',
          requiresAuth: false
        }
      }
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面未找到',
      requiresAuth: false
    }
  }
]

// 需要权限验证的路由
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layouts/DefaultLayout.vue'),
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard/DashboardView.vue'),
        meta: {
          title: '仪表盘',
          icon: 'dashboard',
          permission: 'dashboard',
          requiresAuth: true
        }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/Profile/ProfileView.vue'),
        meta: {
          title: '个人中心',
          icon: 'user',
          permission: 'profile',
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/system',
    name: 'System',
    component: () => import('@/layouts/DefaultLayout.vue'),
    redirect: '/system/user',
    meta: {
      title: '系统管理',
      icon: 'setting',
      permission: 'system',
      requiresAuth: true
    },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/User/UserView.vue'),
        meta: {
          title: '用户管理',
          icon: 'user',
          permission: 'system:user',
          requiresAuth: true
        }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/Role/RoleView.vue'),
        meta: {
          title: '角色管理',
          icon: 'team',
          permission: 'system:role',
          requiresAuth: true
        }
      },
      {
        path: 'permission',
        name: 'Permission',
        component: () => import('@/views/Permission/PermissionView.vue'),
        meta: {
          title: '权限管理',
          icon: 'safety',
          permission: 'system:permission',
          requiresAuth: true
        }
      },
      {
        path: 'customer',
        name: 'Customer',
        component: () => import('@/views/Customer/CustomerView.vue'),
        meta: {
          title: '客户管理',
          icon: 'team',
          permission: 'system:customer',
          requiresAuth: true
        }
      },
      {
        path: 'product',
        name: 'Product',
        component: () => import('@/views/Product/ProductView.vue'),
        meta: {
          title: '产品管理',
          icon: 'shop',
          permission: 'system:product',
          requiresAuth: true
        }
      },
      {
        path: 'quote',
        name: 'Quote',
        component: () => import('@/views/Quote/QuoteView.vue'),
        meta: {
          title: '报价单管理',
          icon: 'file-text',
          permission: 'system:quote',
          requiresAuth: true
        }
      },
      {
        path: 'sales-order',
        name: 'SalesOrder',
        component: () => import('@/views/SalesOrder/SalesOrderView.vue'),
        meta: {
          title: '销售订单管理',
          icon: 'shopping',
          permission: 'system:sales-order',
          requiresAuth: true
        }
      },
      {
        path: 'sales-statistics',
        name: 'SalesStatistics',
        component: () => import('@/views/SalesOrder/SalesStatisticsView.vue'),
        meta: {
          title: '销售统计',
          icon: 'bar-chart',
          permission: 'system:sales-statistics',
          requiresAuth: true
        }
      }
    ]
  },
  // 404页面必须放在最后
  { 
    path: '/:pathMatch(.*)*', 
    redirect: '/404',
    meta: { 
      hidden: true 
    }
  }
]

// 导出所有路由
const routes = [...constantRoutes, ...asyncRoutes]
export default routes
